home *** CD-ROM | disk | FTP | other *** search
/ Aminet 31 / Aminet 31 (1999)(Schatztruhe)[!][Jun 1999].iso / Aminet / comm / thor / thor26_api.lha / ScriptServ / ScriptServ.c < prev    next >
C/C++ Source or Header  |  1995-08-20  |  16KB  |  661 lines

  1. /* ScriptServ.c
  2.     Auto: smake -f src:bbsread/ScriptServ/makefile
  3. */
  4.  
  5. #include <exec/types.h>
  6. #include <exec/memory.h>
  7. #include <exec/nodes.h>
  8. #include <exec/lists.h>
  9. #include <exec/semaphores.h>
  10. #include <libraries/dos.h>
  11. #include <dos/exall.h>
  12. #include <utility/tagitem.h>
  13. #include <proto/exec.h>
  14. #include <proto/dos.h>
  15. #include <proto/utility.h>
  16.  
  17. #include <stdio.h> 
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <ctype.h>
  21.  
  22. #include <libraries/bbsread.h>
  23. #include <proto/bbsread.h>
  24.  
  25. #define USESTRLEN                400L
  26. #define SIZE_EXALLBUFFER    256L
  27. #define CONFIG_VERSION        4L
  28.  
  29. /*  Global structure to hold librarybases  */
  30. struct TaskData
  31. {
  32.     struct ExecBase *SysBase;
  33.     struct DosLibrary *DOSBase;
  34.     struct Library *BBSReadBase;
  35.     struct Library *UtilityBase;
  36. };
  37.  
  38. #define SysBase        td->SysBase
  39. #define DOSBase        td->DOSBase
  40. #define BBSReadBase    td->BBSReadBase
  41. #define UtilityBase    td->UtilityBase
  42.  
  43. extern char *progname, *progdate;
  44. extern LONG *version, *revision;
  45.  
  46. /*  Prototypes for external functions  */
  47. extern APTR SPrintf(STRPTR dest, STRPTR fmtstr, ...);
  48. extern struct Library *OpenBRLibrary(struct ExecBase *, struct DosLibrary *, LONG);
  49.  
  50. /*  Prototypes for global functions  */
  51. struct Node * FindiName(struct List *list, STRPTR name);
  52. UBYTE *StripAnsi(UBYTE *s);
  53.  
  54. UBYTE *template = "BBSNAME,EVENT=EV/N/K/M,DONE/K,ERROR/K,WRITECFG/S,PACKAGEDONE/S,RENAMEGRAB/S,PRIVUPLOADFILE/K,UPDATEFILENOTES/K,USAGE/S";
  55.  
  56. enum {
  57.     TEM_BBSNAME,
  58.     TEM_EVENT,
  59.     TEM_DONE,
  60.     TEM_ERROR,
  61.     TEM_WRITECFG,
  62.     TEM_PACKAGEDONE,
  63.     TEM_RENAMEGRAB,
  64.     TEM_PRIVUPLOADFILE,
  65.     TEM_UPDATEFILENOTES,
  66.     TEM_USAGE,
  67.     TEM_NUMBEROF
  68. };
  69.  
  70. /*
  71.    Format of configuration file:
  72. VERSION        (4)
  73. BBSID
  74. BBSTYPE
  75. BBSDIR
  76. POSTCONF
  77. UPLOADDIR
  78. USERNAME
  79. USERSTREET
  80. USERADDRESS
  81. USERCOUNTRY
  82. USERPHONE
  83. NEWFILES            (y/n)
  84. AUTOPRIVDNL        (y/n)
  85. AUTOLOGOFF        (y/n)
  86. USECOLORS        (y/n)
  87. ANSIMENUES        (y/n)
  88. BULLETINS        (y/n)
  89. REPLYPACKET
  90. */
  91.  
  92. /*
  93.    Format of event file:
  94. EVENTNR        <- Use when marking done/error
  95. EVENT
  96. <tags in same order as in type config>
  97. */
  98.  
  99. STRPTR fmtstr =
  100. "%s V%ld.%ld\n"
  101. "Usage: %s [bbsname] [DONE eventnr] [ERROR eventnr] [WRITECFG]\n"
  102. "          [PACKAGEDONE] [RENAMEGRAB] [PrivUploadFile file]\n"
  103. "          [UpdateFileNotes file] [USAGE]\n"
  104. "Copyright © Ultima Thule Software %s  Author: Eivind Nordseth\n%s";
  105.  
  106. LONG __saveds NoName(void)
  107. {
  108.     struct TaskData TaskData, *td;
  109.     struct RDArgs *rdargs = NULL, *args = NULL;
  110.     STRPTR errmsg = NULL;
  111.     LONG array[TEM_NUMBEROF], retval = RETURN_FAIL, n;
  112.     struct TypeListItem *mytype = NULL;
  113.     struct BBSListItem *mybbs;
  114.     struct ConfListItem *myconf;
  115.     struct BBSData *bbsdata;
  116.     struct BBSInternal *bbsint;
  117.     struct UserData *userd = NULL;
  118.     struct List *bbslist = NULL, *conflist = NULL;
  119.     struct GlobalConfig *globalcfg = NULL;
  120.     struct ExAllControl *eac = NULL;
  121.     struct ExAllData *ead, *buffer = NULL;
  122.     STRPTR tobuf = NULL, confbuf = NULL, uplpath;
  123.     APTR eveobj = NULL;
  124.     ULONG eventnr, tobuflen = 0, confbuflen = 0;
  125.     TEXT filename[USESTRLEN];
  126.     BPTR fh = NULL, lock = NULL;
  127.     BYTE endchr;
  128.  
  129.     setmem(td = &TaskData, sizeof(struct TaskData), 0);
  130.     SysBase = *(struct ExecBase **)4;
  131.     
  132.     if(!(DOSBase = (struct DosLibrary *) OpenLibrary(DOSNAME, 0L))) return(10000L);
  133.     if(DOSBase->dl_lib.lib_Version < 37)
  134.     {
  135.         errmsg = "Needs Kick V37+";
  136.         goto quit;
  137.     }
  138.     if(!(UtilityBase = OpenLibrary("utility.library", 0L))) return(10000L);
  139.  
  140.     if(!(BBSReadBase = OpenBRLibrary(SysBase, DOSBase, 3L)))
  141.     {
  142.         errmsg = "Needs bbsread.library V3+";
  143.         goto quit;
  144.     }
  145.  
  146.      setmem(array, sizeof(ULONG) * TEM_NUMBEROF, '\0'); 
  147.  
  148.     if(!(args = AllocDosObject(DOS_RDARGS, NULL))) goto quit;
  149.  
  150.     SPrintf(args->RDA_ExtHelp = filename, fmtstr, progname, version, revision, 
  151.         progname, progdate, template);
  152.         
  153. /*    Printf("BufUsage: %ld\n", strlen(filename)); */
  154.  
  155.     if(!(rdargs = ReadArgs(template, array, args)))
  156.     {
  157.         errmsg = "Error in arguments.";
  158.         goto quit;
  159.     }
  160.  
  161.     if(array[TEM_USAGE] || !array[TEM_BBSNAME]) 
  162.     {
  163.         Printf(fmtstr, progname, version, revision, progname, progdate, "");
  164.         retval = RETURN_OK;
  165.         goto quit;
  166.     }
  167.     
  168.     if(!(globalcfg = GetGlobalConfig())) { errmsg = "Error geting global configuration."; goto quit; }
  169.  
  170.     if(!(bbslist = GetBBSList())) { errmsg = "Error geting bbslist."; goto quit; }
  171.  
  172.     if(!(mybbs = (struct BBSListItem *) FindiName(bbslist, (STRPTR) array[TEM_BBSNAME])))
  173.     {
  174.         errmsg = "Unknown BBS";
  175.         goto quit;
  176.     }
  177.  
  178.     if(!(mytype = TypeFromBBS(mybbs))) { errmsg =  "Error geting type."; goto quit; }
  179.  
  180.     if(!(conflist = GetConfList(mybbs))) { errmsg = "Error geting conflist."; goto quit; }
  181.  
  182.     bbsdata = mybbs->bl_Data;
  183.     bbsint  = mybbs->bl_Internal;
  184.  
  185.     if(array[TEM_DONE])
  186.     {
  187.         STRPTR ptr = (STRPTR) array[TEM_DONE];
  188.         ULONG flags;
  189.         APTR obj;
  190.  
  191.         eventnr = atol(ptr);
  192.         
  193.         while(isdigit(*ptr)) ptr++;
  194.  
  195.         if(!*ptr)
  196.         {
  197.             if(obj = ReadBREventTags(mybbs, eventnr, RBREV_Flags, &flags, TAG_END))
  198.             {
  199.                 if(!(flags & EDF_ERROR)) UpdateBREventTags(mybbs, eventnr, UBRE_SetDone, TRUE, TAG_END);
  200.                 FreeBRObject(obj);
  201.             }
  202.         }
  203.     }
  204.  
  205.     if(array[TEM_ERROR])
  206.     {
  207.         eventnr = atol((STRPTR) array[TEM_ERROR]);
  208.  
  209.         UpdateBREventTags(mybbs, eventnr, UBRE_SetError, TRUE, TAG_END);
  210.     }
  211.     
  212.     if(array[TEM_WRITECFG])
  213.     {
  214.         myconf = (struct ConfListItem *) &conflist->lh_Head;
  215.  
  216.         SPrintf(filename, "t:%s.CONFIG", bbsdata->bd_Name); 
  217.         if(!(fh = Open(filename, MODE_NEWFILE))) 
  218.         {
  219.             errmsg = "Unable to create config file";
  220.             goto quit;
  221.         }
  222.         FPrintf(fh, "%ld\n%s\n%s\n%s\n",
  223.             CONFIG_VERSION,
  224.             bbsdata->bd_GrabName,
  225.             mytype->tl_Data->td_TypeName,
  226.             mybbs->bl_BBSPath);
  227.         
  228.         while((myconf = (struct ConfListItem *) myconf->cl_Node.ln_Succ)->cl_Node.ln_Succ)
  229.         {
  230.             if(myconf->cl_Data->cd_Flags & CDF_MAIL) break;
  231.         }
  232.         if(!myconf->cl_Node.ln_Succ)
  233.         {
  234.             if(myconf = (struct ConfListItem *) conflist->lh_Head)
  235.             {
  236.                 if(myconf->cl_Node.ln_Succ) myconf = (struct ConfListItem *) myconf->cl_Node.ln_Succ;
  237.             }
  238.         }
  239.         if(myconf->cl_Node.ln_Succ) FPuts(fh, myconf->cl_Data->cd_Name); 
  240.         else FPuts(fh, "Post");
  241.         FPutC(fh,'\n');
  242.  
  243.         if(!(uplpath = bbsdata->bd_UploadPath)) 
  244.             if(!(uplpath = globalcfg->gc_UploadPath)) uplpath = "ram:";
  245.         
  246.         FPuts(fh, uplpath);
  247.         endchr = uplpath[strlen(uplpath) - 1];
  248.  
  249.         if(!(endchr == ':' || endchr == '/')) FPutC(fh, '/');
  250.  
  251.         if(!(userd = BBSUserData(globalcfg, mybbs)))
  252.         {
  253.             errmsg = "Unable to get BBS User Data";
  254.             goto quit;
  255.         }
  256.  
  257.         FPrintf(fh, "\n%s\n%s\n%s\n%s\n%s\n",
  258.             userd->ud_Name,
  259.             userd->ud_Street,
  260.             userd->ud_Address,
  261.             userd->ud_Country,
  262.             userd->ud_Phone);
  263.  
  264.         FPrintf(fh, "%lc\n%lc\n%lc\n%lc\n%lc\n%lc\n", 
  265.             bbsdata->bd_ScrFlags & SF_NEWFILES   ? 'y' : 'n',
  266.             bbsdata->bd_ScrFlags & SF_AUTOPDNL   ? 'y' : 'n',
  267.             bbsdata->bd_ScrFlags & SF_AUTOLOGOFF ? 'y' : 'n',
  268.             bbsdata->bd_ScrFlags & SF_USECOLORS  ? 'y' : 'n',
  269.             bbsdata->bd_ScrFlags & SF_ANSIMENUES ? 'y' : 'n',
  270.             bbsdata->bd_ScrFlags & SF_BULLETINS  ? 'y' : 'n');
  271.  
  272.         FPrintf(fh, "%s\n", bbsdata->bd_ReplyPacket ? bbsdata->bd_ReplyPacket : (STRPTR) "");
  273.  
  274.         Close(fh); fh = NULL;
  275.     }
  276.  
  277.     if(array[TEM_PACKAGEDONE])
  278.     {
  279.         for(n = bbsint->bi_FirstEvent; n <= bbsint->bi_LastEvent; n++)
  280.         {
  281.             ULONG flags;
  282.             APTR obj;
  283.  
  284.             if(obj = ReadBREventTags(mybbs, n, RBREV_Flags, &flags, TAG_END))
  285.             {
  286.                 if(flags & EDF_PACKED) UpdateBREventTags(mybbs, n, UBRE_SetDone, TRUE, TAG_END);
  287.                 FreeBRObject(obj);
  288.             }
  289.         }
  290.         PackDataFileTags(PD_EventData, mybbs, TAG_DONE);
  291.     }
  292.     
  293.     if(array[TEM_RENAMEGRAB])
  294.     {
  295.         STRPTR ptr;
  296.         BOOL more, match;
  297.  
  298.         if(!(lock = Lock(globalcfg->gc_DnloadPath, ACCESS_READ))) goto quit;
  299.         if(!(buffer  = AllocMem(SIZE_EXALLBUFFER, MEMF_ANY))) goto quit;
  300.         if(!(eac = AllocDosObject(DOS_EXALLCONTROL, NULL))) goto quit;
  301.         
  302.         eac->eac_LastKey = (ULONG) (eac->eac_MatchString = (UBYTE *) (eac->eac_MatchFunc = NULL));
  303.         
  304.         do
  305.         {
  306.             more = ExAll(lock, buffer, SIZE_EXALLBUFFER, ED_TYPE, eac);
  307.             if((!more) && (IoErr() != ERROR_NO_MORE_ENTRIES)) goto quit;
  308.  
  309.             if(eac->eac_Entries == 0) continue;
  310.  
  311.             for(ead = buffer; ead; ead = ead->ed_Next)
  312.             {
  313.                 if(ead->ed_Type > 0) continue;
  314.  
  315.                 match = !strnicmp(bbsdata->bd_GrabName, ead->ed_Name, strlen(bbsdata->bd_GrabName));
  316.                 ptr = &ead->ed_Name[strlen(bbsdata->bd_GrabName)];
  317.  
  318.                 if(match && (*ptr == '.' || *ptr == '\0'))
  319.                 {
  320.                     BPTR dlock, exist;
  321.                     LONG num = 0;
  322.                     
  323.                     dlock = CurrentDir(lock);
  324.                     do
  325.                     {
  326.                         num++;
  327.                         SPrintf(filename, "%s%ld%s", bbsdata->bd_G